home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / tools / bison.lha / bison++-1.04 / bison++.simple.cc < prev    next >
C/C++ Source or Header  |  1991-03-28  |  11KB  |  450 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2.  
  3. /* Skeleton output parser for bison++,
  4.    Copyright (C) 1984 Bob Corbett and Richard Stallman
  5.    Copyright (C) 1989 Larry M. Augustin
  6.  
  7.    Adapted from the original for C++ by Larry M. Augustin.
  8.  
  9.    This program is free software; you can redistribute it and/or modify
  10.    it under the terms of the GNU General Public License as published by
  11.    the Free Software Foundation; either version 1, or (at your option)
  12.    any later version.
  13.  
  14.    This program is distributed in the hope that it will be useful,
  15.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.    GNU General Public License for more details.
  18.  
  19.    You should have received a copy of the GNU General Public License
  20.    along with this program; if not, write to the Free Software
  21.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  22.  
  23.  
  24. /* This is the parser code that is written into each bison parser
  25.   when the %semantic_parser declaration is not specified in the grammar.
  26.   It was written by Richard Stallman by simplifying the hairy parser
  27.   used when %semantic_parser is specified.  */
  28.  
  29. /* Note: there must be only one dollar sign in this file.
  30.    It is replaced by the list of actions, each action
  31.    as one case of the switch.  */
  32.  
  33. #define yyerrok        (yyerrstatus = 0)
  34. #define yyclearin    (yychar = YYEMPTY)
  35. #define YYEMPTY        -2
  36. #define YYEOF        0
  37. #define YYFAIL        goto yyerrlab;
  38. #define YYACCEPT    return(0)
  39. #define YYABORT     return(1)
  40. #define YYERROR        goto yyerrlab
  41.  
  42. #define YYTERROR    1
  43. #define YYERRCODE    256
  44.  
  45. #ifndef YYIMPURE
  46. #define YYLEX        yylex()
  47. #endif
  48.  
  49. #ifndef YYPURE
  50. #define YYLEX        yylex(&yylval, &yylloc)
  51. #endif
  52.  
  53. /*  YYMAXDEPTH indicates the initial size of the parser's stacks    */
  54.  
  55. #ifndef    YYMAXDEPTH
  56. #define YYMAXDEPTH 200
  57. #endif
  58.  
  59. /*  YYMAXLIMIT is the maximum size the stacks can grow to
  60.     (effective only if the built-in stack extension method is used).  */
  61.  
  62. #ifndef YYMAXLIMIT
  63. #define YYMAXLIMIT 10000
  64. #endif
  65.  
  66. int @::yyparse() {
  67.   register int yystate;
  68.   register int yyn;
  69.   register short *yyssp;
  70.   register @_YYSTYPE *yyvsp;
  71.   @_YYLTYPE *yylsp;
  72.   int yyerrstatus;  // number of tokens to shift before error messages enabled
  73.   int yychar1;        // lookahead token as an internal (translated) token number
  74.  
  75.   short    yyssa[YYMAXDEPTH];    //  the state stack
  76.   @_YYSTYPE yyvsa[YYMAXDEPTH];    //  the semantic value stack
  77.   @_YYLTYPE yylsa[YYMAXDEPTH];    //  the location stack
  78.  
  79.   short *yyss = yyssa;       //  refer to the stacks thru separate pointers
  80.   @_YYSTYPE *yyvs = yyvsa;   //  to allow yyoverflow to reallocate them elsewhere
  81.   @_YYLTYPE *yyls = yylsa;
  82.  
  83.   int yymaxdepth = YYMAXDEPTH;
  84.  
  85. #ifndef YYPURE
  86.   int yychar;
  87.   @_YYSTYPE yylval;
  88.   @_YYLTYPE yylloc;
  89.   int yynerrs;
  90. #endif
  91.  
  92.   @_YYSTYPE yyval;        /*  the variable used to return        */
  93.                 /*  semantic values from the action    */
  94.                 /*  routines                */
  95.  
  96.   int yylen;
  97.  
  98. #if YYDEBUG != 0
  99.   if (yydebug)
  100.     fprintf(stderr, "Starting parse\n");
  101. #endif
  102.  
  103.   yystate = 0;
  104.   yyerrstatus = 0;
  105.   yynerrs = 0;
  106.   yychar = YYEMPTY;        /* Cause a token to be read.  */
  107.  
  108.   /* Initialize stack pointers.
  109.      Waste one element of value and location stack
  110.      so that they stay on the same level as the state stack.  */
  111.  
  112.   yyssp = yyss - 1;
  113.   yyvsp = yyvs;
  114.   yylsp = yyls;
  115.  
  116. /* Push a new state, which is found in  yystate  .  */
  117. /* In all cases, when you get here, the value and location stacks
  118.    have just been pushed. so pushing a state here evens the stacks.  */
  119. yynewstate:
  120.  
  121.   *++yyssp = yystate;
  122.  
  123.   if (yyssp >= yyss + yymaxdepth - 1)
  124.     {
  125.       /* Give user a chance to reallocate the stack */
  126.       /* Use copies of these so that the &'s don't force the real ones into memory. */
  127.       @_YYSTYPE *yyvs1 = yyvs;
  128.       @_YYLTYPE *yyls1 = yyls;
  129.       short *yyss1 = yyss;
  130.  
  131.       /* Get the current used size of the three stacks, in elements.  */
  132.       int size = yyssp - yyss + 1;
  133.  
  134. #ifdef yyoverflow
  135.       /* Each stack pointer address is followed by the size of
  136.      the data in use in that stack, in bytes.  */
  137.       yyoverflow("parser stack overflow",
  138.          &yyss1, size * sizeof (*yyssp),
  139.          &yyvs1, size * sizeof (*yyvsp),
  140.          &yyls1, size * sizeof (*yylsp),
  141.          &yymaxdepth);
  142.  
  143.       yyss = yyss1; yyvs = yyvs1; yyls = yyls1;
  144. #else /* no yyoverflow */
  145.       /* Extend the stack our own way.  */
  146.       if (yymaxdepth >= YYMAXLIMIT)
  147.     yyerror("parser stack overflow");
  148.       yymaxdepth *= 2;
  149.       if (yymaxdepth > YYMAXLIMIT)
  150.     yymaxdepth = YYMAXLIMIT;
  151.       yyss = (short *) alloca (yymaxdepth * sizeof (*yyssp));
  152.       bcopy ((char *)yyss1, (char *)yyss, size * sizeof (*yyssp));
  153.       yyvs = (@_YYSTYPE *) alloca (yymaxdepth * sizeof (*yyvsp));
  154.       bcopy ((char *)yyvs1, (char *)yyvs, size * sizeof (*yyvsp));
  155. #ifdef YYLSP_NEEDED
  156.       yyls = (@_YYLTYPE *) alloca (yymaxdepth * sizeof (*yylsp));
  157.       bcopy ((char *)yyls1, (char *)yyls, size * sizeof (*yylsp));
  158. #endif // YYLSP_NEEDED
  159. #endif /* no yyoverflow */
  160.  
  161.       yyssp = yyss + size - 1;
  162.       yyvsp = yyvs + size - 1;
  163. #ifdef YYLSP_NEEDED
  164.       yylsp = yyls + size - 1;
  165. #endif
  166.  
  167. #if YYDEBUG != 0
  168.       if (yydebug)
  169.     fprintf(stderr, "Stack size increased to %d\n", yymaxdepth);
  170. #endif
  171.  
  172.       if (yyssp >= yyss + yymaxdepth - 1)
  173.     YYABORT;
  174.     }
  175.  
  176. #if YYDEBUG != 0
  177.   if (yydebug)
  178.     fprintf(stderr, "Entering state %d\n", yystate);
  179. #endif
  180.  
  181. /* Do appropriate processing given the current state.  */
  182. /* Read a lookahead token if we need one and don't already have one.  */
  183.  
  184.   /* First try to decide what to do without reference to lookahead token.  */
  185.  
  186.   yyn = yypact[yystate];
  187.   if (yyn == YYFLAG)
  188.     goto yydefault;
  189.  
  190.   /* Not known => get a lookahead token if don't already have one.  */
  191.  
  192.   /* yychar is either YYEMPTY or YYEOF
  193.      or a valid token in external form.  */
  194.  
  195.   if (yychar == YYEMPTY)
  196.     {
  197. #if YYDEBUG != 0
  198.       if (yydebug)
  199.     fprintf(stderr, "Reading a token: ");
  200. #endif
  201.       yychar = YYLEX;
  202.     }
  203.  
  204.   /* Convert token to internal form (in yychar1) for indexing tables with */
  205.  
  206.   if (yychar <= 0)        /* This means end of input. */
  207.     {
  208.       yychar1 = 0;
  209.       yychar = YYEOF;        /* Don't call YYLEX any more */
  210.  
  211. #if YYDEBUG != 0
  212.       if (yydebug)
  213.     fprintf(stderr, "Now at end of input.\n");
  214. #endif
  215.     }
  216.   else
  217.     {
  218.       yychar1 = YYTRANSLATE(yychar);
  219.  
  220. #if YYDEBUG != 0
  221.       if (yydebug)
  222.     fprintf(stderr, "Next token is %d (%s)\n", yychar, yytname[yychar1]);
  223. #endif
  224.     }
  225.  
  226.   yyn += yychar1;
  227.   if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != yychar1)
  228.     goto yydefault;
  229.  
  230.   yyn = yytable[yyn];
  231.  
  232.   /* yyn is what to do for this token type in this state.
  233.      Negative => reduce, -yyn is rule number.
  234.      Positive => shift, yyn is new state.
  235.        New state is final state => don't bother to shift,
  236.        just return success.
  237.      0, or most negative number => error.  */
  238.  
  239.   if (yyn < 0)
  240.     {
  241.       if (yyn == YYFLAG)
  242.     goto yyerrlab;
  243.       yyn = -yyn;
  244.       goto yyreduce;
  245.     }
  246.   else if (yyn == 0)
  247.     goto yyerrlab;
  248.  
  249.   if (yyn == YYFINAL)
  250.     YYACCEPT;
  251.  
  252.   /* Shift the lookahead token.  */
  253.  
  254. #if YYDEBUG != 0
  255.   if (yydebug)
  256.     fprintf(stderr, "Shifting token %d (%s), ", yychar, yytname[yychar1]);
  257. #endif
  258.  
  259.   /* Discard the token being shifted unless it is eof.  */
  260.   if (yychar != YYEOF)
  261.     yychar = YYEMPTY;
  262.  
  263.   *++yyvsp = yylval;
  264. #ifdef YYLSP_NEEDED
  265.   *++yylsp = yylloc;
  266. #endif
  267.  
  268.   /* count tokens shifted since error; after three, turn off error status.  */
  269.   if (yyerrstatus) yyerrstatus--;
  270.  
  271.   yystate = yyn;
  272.   goto yynewstate;
  273.  
  274. /* Do the default action for the current state.  */
  275. yydefault:
  276.  
  277.   yyn = yydefact[yystate];
  278.   if (yyn == 0)
  279.     goto yyerrlab;
  280.  
  281. /* Do a reduction.  yyn is the number of a rule to reduce with.  */
  282. yyreduce:
  283.   yylen = yyr2[yyn];
  284.   yyval = yyvsp[1-yylen]; /* implement default value of the action */
  285.  
  286. #if YYDEBUG != 0
  287.   if (yydebug)
  288.     {
  289.       if (yylen == 1)
  290.     fprintf (stderr, "Reducing 1 value via line %d, ",
  291.          yyrline[yyn]);
  292.       else
  293.     fprintf (stderr, "Reducing %d values via line %d, ",
  294.          yylen, yyrline[yyn]);
  295.     }
  296. #endif
  297.  
  298. $   /* the action file gets copied in in place of this dollarsign */
  299.  
  300.  
  301.   yyvsp -= yylen;
  302.   yyssp -= yylen;
  303. #ifdef YYLSP_NEEDED
  304.   yylsp -= yylen;
  305. #endif
  306.  
  307. #if YYDEBUG != 0
  308.   if (yydebug)
  309.     {
  310.       short *ssp1 = yyss - 1;
  311.       fprintf (stderr, "state stack now");
  312.       while (ssp1 != yyssp)
  313.     fprintf (stderr, " %d", *++ssp1);
  314.       fprintf (stderr, "\n");
  315.     }
  316. #endif
  317.  
  318.   *++yyvsp = yyval;
  319.  
  320. #ifdef YYLSP_NEEDED
  321.   yylsp++;
  322.   if (yylen == 0)
  323.     {
  324.       yylsp->first_line = yylloc.first_line;
  325.       yylsp->first_column = yylloc.first_column;
  326.       yylsp->last_line = (yylsp-1)->last_line;
  327.       yylsp->last_column = (yylsp-1)->last_column;
  328.       yylsp->text = 0;
  329.     }
  330.   else
  331.     {
  332.       yylsp->last_line = (yylsp+yylen-1)->last_line;
  333.       yylsp->last_column = (yylsp+yylen-1)->last_column;
  334.     }
  335. #endif
  336.  
  337.   /* Now "shift" the result of the reduction.
  338.      Determine what state that goes to,
  339.      based on the state we popped back to
  340.      and the rule number reduced by.  */
  341.  
  342.   yyn = yyr1[yyn];
  343.  
  344.   yystate = yypgoto[yyn - YYNTBASE] + *yyssp;
  345.   if (yystate >= 0 && yystate <= YYLAST && yycheck[yystate] == *yyssp)
  346.     yystate = yytable[yystate];
  347.   else
  348.     yystate = yydefgoto[yyn - YYNTBASE];
  349.  
  350.   goto yynewstate;
  351.  
  352. yyerrlab:   /* here on detecting error */
  353.  
  354.   if (! yyerrstatus)
  355.     /* If not already recovering from an error, report this error.  */
  356.     {
  357.       ++yynerrs;
  358.       yyerror("parse error");
  359.     }
  360.  
  361.   if (yyerrstatus == 3)
  362.     {
  363.       /* if just tried and failed to reuse lookahead token after an error, discard it.  */
  364.  
  365.       /* return failure if at end of input */
  366.       if (yychar == YYEOF)
  367.     YYABORT;
  368.  
  369. #if YYDEBUG != 0
  370.       if (yydebug)
  371.     fprintf(stderr, "Discarding token %d (%s).\n", yychar, yytname[yychar1]);
  372. #endif
  373.  
  374.       yychar = YYEMPTY;
  375.     }
  376.  
  377.   /* Else will try to reuse lookahead token
  378.      after shifting the error token.  */
  379.  
  380.   yyerrstatus = 3;        /* Each real token shifted decrements this */
  381.  
  382.   goto yyerrhandle;
  383.  
  384. yyerrdefault:  /* current state does not do anything special for the error token. */
  385.  
  386. #if 0
  387.   /* This is wrong; only states that explicitly want error tokens
  388.      should shift them.  */
  389.   yyn = yydefact[yystate];  /* If its default is to accept any token, ok.  Otherwise pop it.*/
  390.   if (yyn) goto yydefault;
  391. #endif
  392.  
  393. yyerrpop:   /* pop the current state because it cannot handle the error token */
  394.  
  395.   if (yyssp == yyss) YYABORT;
  396.   yyvsp--;
  397.   yystate = *--yyssp;
  398. #ifdef YYLSP_NEEDED
  399.   yylsp--;
  400. #endif
  401.  
  402. #if YYDEBUG != 0
  403.   if (yydebug)
  404.     {
  405.       short *ssp1 = yyss - 1;
  406.       fprintf (stderr, "Error: state stack now");
  407.       while (ssp1 != yyssp)
  408.     fprintf (stderr, " %d", *++ssp1);
  409.       fprintf (stderr, "\n");
  410.     }
  411. #endif
  412.  
  413. yyerrhandle:
  414.  
  415.   yyn = yypact[yystate];
  416.   if (yyn == YYFLAG)
  417.     goto yyerrdefault;
  418.  
  419.   yyn += YYTERROR;
  420.   if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != YYTERROR)
  421.     goto yyerrdefault;
  422.  
  423.   yyn = yytable[yyn];
  424.   if (yyn < 0)
  425.     {
  426.       if (yyn == YYFLAG)
  427.     goto yyerrpop;
  428.       yyn = -yyn;
  429.       goto yyreduce;
  430.     }
  431.   else if (yyn == 0)
  432.     goto yyerrpop;
  433.  
  434.   if (yyn == YYFINAL)
  435.     YYACCEPT;
  436.  
  437. #if YYDEBUG != 0
  438.   if (yydebug)
  439.     fprintf(stderr, "Shifting error token, ");
  440. #endif
  441.  
  442.   *++yyvsp = yylval;
  443. #ifdef YYLSP_NEEDED
  444.   *++yylsp = yylloc;
  445. #endif
  446.  
  447.   yystate = yyn;
  448.   goto yynewstate;
  449. }
  450.